package com.ycdx.zwzc.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.log4j.Logger;

import com.ycdx.zwzc.pojo.Request;
import com.ycdx.zwzc.utils.DBPool;

public class QueryImpl {
	
	static Logger logger = Logger.getLogger(QueryImpl.class.getName());
	
	/**
	 * 根据开始日期，结束日期查询工作量
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Request> queryByDate(String startTime,String endTime){
		List<Request> allRequests = new ArrayList<Request>();
		String sql = "select * from tb_request where " +
				"UNIX_TIMESTAMP(time)<UNIX_TIMESTAMP(ADDDATE(?, INTERVAL 1 DAY)) and UNIX_TIMESTAMP(time)>UNIX_TIMESTAMP(?) and status='已归档'" +
				"order by time";
		
		Object[] params = {endTime,startTime};
		Connection conn = DBPool.getInstance().getConnection();
		QueryRunner qr = new QueryRunner();
		try {
			allRequests = (List<Request>) qr.query(conn,sql, new BeanListHandler(Request.class), params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error("QueryImpl.class-queryByDate()-数据库操作失败！");
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error("QueryImpl.class-queryByDate()-连接关闭失败！");
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		return allRequests;
	}
}
